package com.zdy.controller;

import com.zdy.pojo.Books;
import com.zdy.service.BookService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 逸足天涯
 * on 3/13/2020.
 */
@Controller
@RequestMapping("/book")
public class BookController {
    @Autowired
    @Qualifier("bookServiceImpl")
    private BookService bookService;

    //查询全部的书籍，并且返回到一个书籍展示页面
    @RequestMapping("allBook")
    public String queryAllBook(Model model){
        List<Books> list = bookService.queryAllBooks();
        /*使用Lambda表达式删除结果*/
        list.forEach((Books)-> System.out.println(Books));
        model.addAttribute("list",list);

        return "allBook";
    }

    //跳转到添加书籍页面
    @RequestMapping("toAddBook")
    public String toAddPaper() {
        return "addBook";
    }
    @RequestMapping("addBook")
    public String addBook(Books books){
        System.out.println(books);
        bookService.addbook(books);
        return "redirect:/book/allBook";//重定向到allBook

    }
    //跳转到添加书籍页面
    @RequestMapping("toUpdateBook")
    public String toUpdateBook(int id,Model model) {
        Books book = bookService.queryOneBookById(id);
        model.addAttribute("QBook",book);
        return "updateBook";
    }
    @RequestMapping("updateBook")
    public String updateBook(Books books){
        System.out.println("updateBook=>"+books);
        bookService.updateBooks(books);

        return "redirect:/book/allBook";//重定向到allBook

    }



    @RequestMapping("/deleteBook/{bookId}")
    public String deleteBook(@PathVariable("bookId") int id) {
        bookService.deleteBookById(id);
        return "redirect:/book/allBook";
    }
    @RequestMapping("/queryBook")
    public String queryBook( String queryBookName, Model model){
        Books books = bookService.queryBookByName(queryBookName);
        List<Books> list = new ArrayList<>();
        list.add(books);
        model.addAttribute("list",list);
        return "allBook";
    }

}
